function start_tests(){
    for(var i in tests){
        if(tests.hasOwnProperty(i)){
            tests[i]();
        }
    }
}

function cmp(str, exp){
    value_of(str).should_be(exp);
}


var tests = {
    test_basic_creation : function(){
        describe('creation', {
            'before': function(){
                tog.setConfig({
                    newline: true, 
                    indent: true
                })
            },
            'empty':function(){
                var t = Tog();
                value_of(t.html()).should_be('');
            },
            'table tr td': function(){
                var t = Tog('table').Tog('tr').Tog('td');
                cmp(t.html(), "<table>\n  <tr>\n    <td></td>\n  </tr>\n</table>");
            },
            'table tr td with spec funcs': function(){
                var t = Tog().Table().Tr().Td();
                value_of(t.html()).should_be("<table>\n  <tr>\n    <td></td>\n  </tr>\n</table>");
            },
            'table tr td*3':function(){
                var t = Tog().Table().Tr().td().td().td();
                cmp(t.html(), "<table>\n  <tr>\n    <td></td>\n    <td></td>\n    <td></td>\n  </tr>\n</table>");
            },
            'html tags #1': function(){
                var t = Tog().Div('#div_id.div_class')
                .Span('#span1.span1.span1_2')
                .span('.span1/.span2')
                .span('.span3')
                .Table('#main_tbl&$.common_str')
                .Tr('#tr')
                .td('.td_1').cont('some value')
                .td('.td_2').cont('some other value').attr('test_attr', 'attr = ', 3)
                .Td('#td_cell')
                .b('bold text')
                .br()
                .strong('strong text')
                .br()
                .em('em text')
                .i('i text');

                cmp(t.html(), '<div id="div_id" class="div_class"><span id="span1" class="span1 span1_2"></span><span class="span2"></span><span class="span3"></span>\n  <table id="main_tbl" name="common_str" type="common_str" class="common_str">\n    <tr id="tr">\n      <td class="td_1">some value</td>\n      <td class="td_2" test_attr="attr = 3">some other value</td>\n      <td id="td_cell"><b>bold text</b>\n        <br/>\n<strong>strong text</strong>\n        <br/>\n<em>em text</em><i>i text</i></td>\n    </tr>\n  </table>\n</div>');
            },
            'html tags + attrs #2': function(){
                var t = tog.Table('#main_table').attr('table_name', 'shop_no', 3)
                .cont(
                    tog.Tr('#rec_1').attr('prod_id', 3)
                    .cont(
                        tog.td('.name').cont('choc')
                        .td('.review').cont(23,' people liked it')
                        ),
                    tog.Tr('#res_2').attr('prod_id', 41)
                    .cont(
                        tog.td('.name').cont('candy')
                        .Td('.review').Span('.strongText')
                        .cont('no one').i(' actually ').close().cont('like it')
                        )
                    )
                cmp(t.html(), '<table id="main_table" table_name="shop_no3">\n    <tr id="rec_1" prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\n    </tr>\n</table>');
            },
            'html embeded': function(){
                var t = tog.Table();
                t.map(data.products, function(data, key){
                    return Tog('tr.product_row').attr('prodId', data.id)
                    .td().cont(key + '.')
                    .td('.name').cont(data.name)
                    .td('.price').cont(data.price)
                    .td('.shop').cont(data.shop.name)
                    .td('.location').cont(data.shop.location)
                })
                cmp(t.html(), '<table>\n    <tr class="product_row" prodId="2">\n      <td>0.</td>\n      <td class="name">choc</td>\n      <td class="price">2.32</td>\n      <td class="shop">choc store</td>\n      <td class="location">1 this st. that block</td>\n    </tr>\n    <tr class="product_row" prodId="3">\n      <td>1.</td>\n      <td class="name">choc</td>\n      <td class="price">1</td>\n      <td class="shop">choc store</td>\n      <td class="location">1 this st. that block</td>\n    </tr>\n    <tr class="product_row" prodId="34">\n      <td>2.</td>\n      <td class="name">meat</td>\n      <td class="price">2.12</td>\n      <td class="shop">butcher\'s</td>\n      <td class="location">this ave</td>\n    </tr>\n    <tr class="product_row" prodId="51">\n      <td>3.</td>\n      <td class="name">chinese</td>\n      <td class="price">222.32</td>\n      <td class="shop">no way..</td>\n      <td class="location"></td>\n    </tr>\n    <tr class="product_row" prodId="12">\n      <td>4.</td>\n      <td class="name">candy</td>\n      <td class="price">12.32</td>\n      <td class="shop">candy shop</td>\n      <td class="location">somewhere in somewhere</td>\n    </tr>\n</table>');
            },
            'all inputs': function(){
                var t = Tog().Div('#main_div')
                .input('$name_1', 'new input')
                .input('$name_1', 'new input', 'button')
                .button('$#button_1', 'Save')
                .checkbox('$#checkbox1', 'yes', true)
                .checkbox('$#checkbox2', 'yes', false)
                .checkbox('$#checkbox3', 'yes')
                .radio('$#radio_button', 'male', true)
                .radio('$#radio_button', 'female', false)
                .radio('$#radio_button', 'unknow')
                .Select('#$select_box.my_select')
                .option('val1', 'value 1')
                .option('val2', 'value 2', true)
                .option('val2', 'value 3', false)
                .Select('/$#select_map')
                .map(data.products, function(d, key, e){
                    return tog.option(key, d.name + ' ($' + d.price + ')', e._num == 3? true: false);
                });
                
                cmp(t.html(), '<div id="main_div">\n  <input name="name_1" value="new input" type="text"/>\n  <input name="name_1" value="new input" type="button"/>\n  <input id="button_1" name="button_1" value="Save" type="button"/>\n  <input id="checkbox1" name="checkbox1" value="yes" type="checkbox" checked=""/>\n  <input id="checkbox2" name="checkbox2" value="yes" type="checkbox"/>\n  <input id="checkbox3" name="checkbox3" value="yes" type="checkbox"/>\n  <input id="radio_button" name="radio_button" value="male" type="radio" checked=""/>\n  <input id="radio_button" name="radio_button" value="female" type="radio"/>\n  <input id="radio_button" name="radio_button" value="unknow" type="radio"/>\n  <select id="select_box" name="select_box" class="my_select">\n    <option value="val1">value 1</option>\n    <option value="val2" selected="">value 2</option>\n    <option value="val2">value 3</option>\n  </select>\n  <select id="select_map" name="select_map">\n      <option value="0">choc ($2.32)</option>\n      <option value="1">choc ($1)</option>\n      <option value="2">meat ($2.12)</option>\n      <option value="3" selected="">chinese ($222.32)</option>\n      <option value="4">candy ($12.32)</option>\n  </select>\n</div>');
            }
        })
    // basic tests
    },
    test_clones: function(){
        describe('cloning tog objs', {
            'simple cloning': function(){
                var t = Tog().Div('#&.?$main_div')
                .input('$name_1', 'new input')
                .input('$name_1', 'new input', 'button')
                .button('$#button_1', 'Save')
                .checkbox('$#checkbox1', 'yes', true)
                .checkbox('$#checkbox2', 'yes', false)
                .checkbox('$#checkbox3', 'yes')
                .radio('$#radio_button', 'male', true)
                .radio('$#radio_button', 'female', false)
                .radio('$#radio_button', 'unknow')
                .Select('#$select_box.my_select')
                .option('val1', 'value 1')
                .option('val2', 'value 2', true)
                .option('val2', 'value 3', false)
                .Select('/$#select_map')
                .map(data.products, function(d, key, e){
                    return tog.option(key, d.name + ' ($' + d.price + ')', e._num == 3? true: false);
                });
                
                var r = t.clone();
                
                cmp(r.html(), '<div id="main_div" name="main_div" type="main_div" class="main_div">\n  <input name="name_1" value="new input" type="text"/>\n  <input name="name_1" value="new input" type="button"/>\n  <input id="button_1" name="button_1" value="Save" type="button"/>\n  <input id="checkbox1" name="checkbox1" value="yes" type="checkbox" checked=""/>\n  <input id="checkbox2" name="checkbox2" value="yes" type="checkbox"/>\n  <input id="checkbox3" name="checkbox3" value="yes" type="checkbox"/>\n  <input id="radio_button" name="radio_button" value="male" type="radio" checked=""/>\n  <input id="radio_button" name="radio_button" value="female" type="radio"/>\n  <input id="radio_button" name="radio_button" value="unknow" type="radio"/>\n  <select id="select_box" name="select_box" class="my_select">\n    <option value="val1">value 1</option>\n    <option value="val2" selected="">value 2</option>\n    <option value="val2">value 3</option>\n  </select>\n  <select id="select_map" name="select_map">\n      <option value="0">choc ($2.32)</option>\n      <option value="1">choc ($1)</option>\n      <option value="2">meat ($2.12)</option>\n      <option value="3" selected="">chinese ($222.32)</option>\n      <option value="4">candy ($12.32)</option>\n  </select>\n</div>');
            
            },
            'html tags + attrs #2 cloning': function(){
                var t = tog.Table('#main_table').attr('table_name', 'shop_no', 3)
                .cont(
                    tog.Tr('#rec_1').attr('prod_id', 3)
                    .cont(
                        tog.td('.name').cont('choc')
                        .td('.review').cont(23,' people liked it')
                        ),
                    tog.Tr('#res_2').attr('prod_id', 41)
                    .cont(
                        tog.td('.name').cont('candy')
                        .Td('.review').Span('.strongText')
                        .cont('no one').i(' actually ').close().cont('like it')
                        )
                    )
                cmp(t.clone().html(), '<table id="main_table" table_name="shop_no3">\n    <tr id="rec_1" prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\n    </tr>\n</table>');
                cmp(t.clone().clone().html(), '<table id="main_table" table_name="shop_no3">\n    <tr id="rec_1" prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\n    </tr>\n</table>');
            },
            'find cloning': function(){
                var t = tog.Table('#main_table').attr('table_name', 'shop_no', 3)
                .cont(
                    tog.Tr('#rec_1').attr('prod_id', 3)
                    .cont(
                        tog.td('.name').cont('choc')
                        .td('.review').cont(23,' people liked it')
                        ),
                    tog.Tr('#res_2').attr('prod_id', 41)
                    .cont(
                        tog.td('.name').cont('candy')
                        .Td('.review').Span('.strongText')
                        .cont('no one').i(' actually ').close().cont('like it')
                        )
                    )
                        
                var r = t.clone();
                t.find('/#res_2').cont('more').attr('more', true);
                        
                cmp(r.html(), '<table id="main_table" table_name="shop_no3">\n    <tr id="rec_1" prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\n    </tr>\n</table>');
                cmp(t.html(), '<table id="main_table" table_name="shop_no3">\n    <tr id="rec_1" prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41" more="true">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\nmore</tr>\n</table>');
            }
        })
    },
    test_gets: function(){
        describe('test get ids/ancs & finds', {
            'get by ids': function(){
                var t = tog.Table('#main_table').attr('table_name', 'shop_no', 3)
                .cont(
                    tog.Tr('#rec_1').attr('prod_id', 3)
                    .cont(
                        tog.td('.name').cont('choc')
                        .td('.review').cont(23,' people liked it')
                        ),
                    tog.Tr('#res_2').attr('prod_id', 41)
                    .cont(
                        tog.td('.name').cont('candy')
                        .Td('.review').Span('.strongText')
                        .cont('no one').i(' actually ').close().cont('like it')
                        )
                    );
                        
                t.getById('res_2').attr('name_type', 'test add attr')
                .cont('test add content')
                .Span().cont('test new span')
                .span().cont('test new span 2')
                .up(1)
                .a('http://www.test.com', 'Test link');
                
                cmp(t.html(), '<table id="main_table" table_name="shop_no3">\n    <tr id="rec_1" prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41" name_type="test add attr">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\ntest add content<span>test new span<span>test new span 2</span></span>\n      <a href="http://www.test.com">Test link</a>\n    </tr>\n</table>');
                
                t.getById('main_table').attr('name_type', 'test add attr#')
                .cont('test add content#')
                .Span().cont('test new span#')
                .span().cont('test new span 2#')
                .up()
                .a('http://www.test.com', 'Test link#');
                
                cmp(t.html(), '<table id="main_table" table_name="shop_no3" name_type="test add attr#">\n    <tr id="rec_1" prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41" name_type="test add attr">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\ntest add content<span>test new span<span>test new span 2</span></span>\n      <a href="http://www.test.com">Test link</a>\n    </tr>\ntest add content#<span>test new span#<span>test new span 2#</span></span>\n  <a href="http://www.test.com">Test link#</a>\n</table>');
                
            },
            'get by ancs': function(){
                var t = tog.Table('?main_table').attr('table_name', 'shop_no', 3)
                .cont(
                    tog.Tr('?rec_1').attr('prod_id', 3)
                    .cont(
                        tog.td('.name').cont('choc')
                        .td('.review').cont(23,' people liked it')
                        ),
                    tog.Tr('#?res_2').attr('prod_id', 41)
                    .cont(
                        tog.td('.name').cont('candy')
                        .Td('.review').Span('.strongText')
                        .cont('no one').i(' actually ').close().cont('like it')
                        )
                    );
                        
                t.getByAnc('res_2').attr('name_type', 'test add attr')
                .cont('test add content')
                .Span().cont('test new span')
                .span().cont('test new span 2')
                .up(1)
                .a('http://www.test.com', 'Test link');
                
                cmp(t.html(), '<table table_name="shop_no3">\n    <tr prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41" name_type="test add attr">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\ntest add content<span>test new span<span>test new span 2</span></span>\n      <a href="http://www.test.com">Test link</a>\n    </tr>\n</table>');
                
                t.getByAnc('main_table').attr('name_type', 'test add attr#')
                .cont('test add content#')
                .Span().cont('test new span#')
                .span().cont('test new span 2#')
                .up()
                .a('http://www.test.com', 'Test link#');
                
                cmp(t.html(), '<table table_name="shop_no3" name_type="test add attr#">\n    <tr prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41" name_type="test add attr">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\ntest add content<span>test new span<span>test new span 2</span></span>\n      <a href="http://www.test.com">Test link</a>\n    </tr>\ntest add content#<span>test new span#<span>test new span 2#</span></span>\n  <a href="http://www.test.com">Test link#</a>\n</table>');
                
            },
            'find': function(){
                var t = tog.Table('?main_table').attr('table_name', 'shop_no', 3)
                .cont(
                    tog.Tr('?rec_1').attr('prod_id', 3)
                    .cont(
                        tog.td('.name').cont('choc')
                        .td('.review').cont(23,' people liked it')
                        ),
                    tog.Tr('#?res_2').attr('prod_id', 41)
                    .cont(
                        tog.td('.name').cont('candy')
                        .Td('.review').Span('.strongText')
                        .cont('no one').i(' actually ').close().cont('like it')
                        )
                    );
                        
                t.find('/?res_2').attr('name_type', 'test add attr')
                .cont('test add content')
                .Span().cont('test new span')
                .span().cont('test new span 2')
                .up(1)
                .a('http://www.test.com', 'Test link');
                
                cmp(t.html(), '<table table_name="shop_no3">\n    <tr prod_id="3">\n        <td class="name">choc</td>\n        <td class="review">23 people liked it</td>\n    </tr>\n    <tr id="res_2" prod_id="41" name_type="test add attr">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\ntest add content<span>test new span<span>test new span 2</span></span>\n      <a href="http://www.test.com">Test link</a>\n    </tr>\n</table>');
                
                t.find('/?main_table ?rec_1 .review').attr('name_type', 'test add attr#')
                .cont('test add content#')
                .Span().cont('test new span#')
                .span().cont('test new span 2#')
                .up()
                .a('http://www.test.com', 'Test link#');
                
                cmp(t.html(), '<table table_name="shop_no3">\n    <tr prod_id="3">\n        <td class="name">choc</td>\n        <td class="review" name_type="test add attr#">23 people liked ittest add content#<span>test new span#<span>test new span 2#</span></span>\n          <a href="http://www.test.com">Test link#</a>\n        </td>\n    </tr>\n    <tr id="res_2" prod_id="41" name_type="test add attr">\n        <td class="name">candy</td>\n        <td class="review"><span class="strongText">no one<i> actually </i>like it</span></td>\ntest add content<span>test new span<span>test new span 2</span></span>\n      <a href="http://www.test.com">Test link</a>\n    </tr>\n</table>');
            }
        })
    // end of test gets , find
    }, 
    test_compile: function(){
        describe('test compiling', {
            "compiling template string": function(){
                var str = "<div id='<,$d.div_id,>'>\n\
    <. var i = 0; while(i < 10){.>\n\
    <span val='<,i,>'><,i++,></span>\
    <. } .>\n\
    <a href='<,$d.lnk,>'><,$d.lnk_name,></a>\n\
    <,$t.test2($d, $e),>\n\
    <script src='somescript.js?<,$e.time(),>' tag='<,$e.extra,>' round='<,Math.round(1.22),>'></script>\n\
</div>"
                tog.compile('test1', str);
                
                var html = tog.cmp_test1({
                    div_id: 'div_id_value',
                    lnk: 'http://what.net',
                    lnk_name: 'link to what.net'
                }, {
                    time: function(){
                        return new Date(2011, 6, 11).getTime();
                    },
                    extra: 'extra value'
                })
                
                cmp(html, "<div id='div_id_value'>\n    \n    <span val='0'>0</span>    \n    <span val='1'>1</span>    \n    <span val='2'>2</span>    \n    <span val='3'>3</span>    \n    <span val='4'>4</span>    \n    <span val='5'>5</span>    \n    <span val='6'>6</span>    \n    <span val='7'>7</span>    \n    <span val='8'>8</span>    \n    <span val='9'>9</span>    \n    <a href='http://what.net'>link to what.net</a>\n    \n    <script src='somescript.js?1310306400000' tag='extra value' round='1'></script>\n</div>");
                
                var test = {};
                tog.compile('test1', str, test);
                tog.compile('test2', '<span><, $d.lnk_name ,> => <, $d.lnk ,> : <, $e.extra ,></span>', test);
    
                html = test.test1({
                    div_id: 'div_id_value',
                    lnk: 'http://what.net',
                    lnk_name: 'link to what.net'
                }, {
                    time: function(){
                        return new Date(2011, 6, 11).getTime();
                    },
                    extra: 'extra value'
                })
                
                cmp(html, "<div id='div_id_value'>\n    \n    <span val='0'>0</span>    \n    <span val='1'>1</span>    \n    <span val='2'>2</span>    \n    <span val='3'>3</span>    \n    <span val='4'>4</span>    \n    <span val='5'>5</span>    \n    <span val='6'>6</span>    \n    <span val='7'>7</span>    \n    <span val='8'>8</span>    \n    <span val='9'>9</span>    \n    <a href='http://what.net'>link to what.net</a>\n    <span>link to what.net => http://what.net : extra value</span>\n    <script src='somescript.js?1310306400000' tag='extra value' round='1'></script>\n</div>");
            },
            test_tog_compile: function(){
                var t = tog.Div().id('<,$d.div_id,>')
                .Code('var i = 0')
                .While('i<10', tog.Span().attr('val', '<,i,>').cont('<,i++,>'))
                .a('<,$d.lnk,>', '<,$d.lnk_name,>')
                .Cont('<,$t.test2($d, $e) ,>')
                .Script('', 'somescript.js?<,$e.time(),>').astr('tag="<,$e.extra,>" round="<,Math.round(1.22),>"')
                .compile('test1');
    
                var html = tog.cmp_test1({
                    div_id: 'div_id_value',
                    lnk: 'http://what.net',
                    lnk_name: 'link to what.net'
                }, {
                    time: function(){
                        return new Date(2011, 6, 11).getTime();
                    },
                    extra: 'extra value'
                })
                
                cmp(html, '<div id="div_id_value"><span val="0">0</span><span val="1">1</span><span val="2">2</span><span val="3">3</span><span val="4">4</span><span val="5">5</span><span val="6">6</span><span val="7">7</span><span val="8">8</span><span val="9">9</span>\n  <a href="http://what.net">link to what.net</a>\n\n  <script tag="extra value" round="1" language="text/javascript" href="somescript.js?1310306400000"></script>\n</div>');
                
                var test = {};
                t.compile('test1', test);
                tog.compile('test2', '<span><, $d.lnk_name ,> => <, $d.lnk ,> : <, $e.extra ,></span>', test);
    
                html = test.test1({
                    div_id: 'div_id_value',
                    lnk: 'http://what.net',
                    lnk_name: 'link to what.net'
                }, {
                    time: function(){
                        return new Date(2011, 6, 11).getTime();
                    },
                    extra: 'extra value'
                })
                
                cmp(html,'<div id="div_id_value"><span val="0">0</span><span val="1">1</span><span val="2">2</span><span val="3">3</span><span val="4">4</span><span val="5">5</span><span val="6">6</span><span val="7">7</span><span val="8">8</span><span val="9">9</span>\n  <a href="http://what.net">link to what.net</a>\n<span>link to what.net => http://what.net : extra value</span>\n  <script tag="extra value" round="1" language="text/javascript" href="somescript.js?1310306400000"></script>\n</div>');
            }
        })
        // end of test_compile
    }
}

function d(str){
    console.log(str);
}

var data = {
    products: [
    {
        id: 2,
        name: 'choc',
        price: 2.32,
        shop:{
            name: 'choc store',
            location:'1 this st. that block'
        }
    }
    ,
    {
        id: 3,
        name: 'choc',
        price: 1,
        shop:{
            name: 'choc store',
            location:'1 this st. that block'
        }
    }
    ,
    {
        id: 34,
        name: 'meat',
        price: 2.12,
        shop:{
            name: 'butcher\'s',
            location:'this ave'
        }
    }
    ,
    {
        id: 51,
        name: 'chinese',
        price: 222.32,
        shop:{
            name: 'no way..',
            location: null
        }
    }
    ,
    {
        id: 12,
        name: 'candy',
        price: 12.32,
        shop:{
            name: 'candy shop',
            location:'somewhere in somewhere'
        }
    }
    ,
    ]
}